﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace POSProgram
{
    public partial class source : Form
    {
        const int minimum = 2;
        const int maximum = 99;
        MDIParent mdip;

        public source(MDIParent _mdip)
        {
            InitializeComponent();
            
            mdip = _mdip;
            for (int i = minimum; i < (maximum + 1); i++)
            {
                toolStripComboBox2.Items.Add(i);
                toolStripComboBox3.Items.Add(i);
            }
            toolStripComboBox1.SelectedIndex = 2;
            toolStripComboBox2.SelectedIndex = 2;
            toolStripComboBox3.SelectedIndex = 2;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            mdip.changed = true;
            for (int i = 1; i < dataGridView1.ColumnCount; i++)
                for (int j = 0; j < dataGridView1.RowCount; j++)
                        dataGridView1[i, j].Value = null;
            if (toolStripComboBox1.SelectedIndex == 2)
                for (int i = 1; i <= MDIParent.z; i++)
                    dataGridView1.Columns[i].HeaderText = i.ToString();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            mdip.changed = true;
            Random rnd = new Random();
            if (toolStripComboBox1.SelectedIndex == 2)
                for (int i = 1; i <= MDIParent.z; i++)
                    dataGridView1.Columns[i].HeaderText = i.ToString();
            for (int i = 0; i < (dataGridView1.RowCount); i++)
                for (int j = 1; j < dataGridView1.ColumnCount; j++)
                    if (j < dataGridView1.ColumnCount - 2)
                        dataGridView1[j, i].Value = rnd.Next(2, 10);
                    else if (j < dataGridView1.ColumnCount - 1)
                        dataGridView1[j, i].Value = rnd.Next(6, 20);
                    else
                        dataGridView1[j, i].Value = 10 * rnd.Next(10, 52);
        }

        private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            mdip.changed = true;
            toolStripComboBox2.Enabled = true;
            toolStripComboBox3.Enabled = true;
            dataGridView1.Rows.Clear();
            dataGridView1.Columns.Clear();
            dataGridView1.Columns.Add("Column0", "Наим. процесса");
            dataGridView1.Columns[0].AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.None;
            dataGridView1.Columns[0].Frozen = true;

            switch (toolStripComboBox1.SelectedIndex)
            {
                case 0: //Ритмичный поток
                    dataGridView1.Columns.Add("Column1", "Ритм бригады");
                    dataGridView1.Columns.Add("Column2", "Рабочие");
                    dataGridView1.Columns.Add("Column3", "Стоимость");
                    dataGridView1.Columns[0].Width = 115;
                    dataGridView1.Columns[1].Width = 115;
                    for (int i = 0; i < MDIParent.p; i++)
                        dataGridView1.Rows.Add("Процесс" + (i + 1));
                    dataGridView1[1, 0].Style.BackColor = Color.AntiqueWhite;
                    dataGridView1[2, 0].Style.BackColor = Color.AntiqueWhite;
                    dataGridView1[3, 0].Style.BackColor = Color.AntiqueWhite;
                    break;
                case 1: //Разноритмичный поток
                    dataGridView1.Columns.Add("Column1", "Ритм бригады");
                    dataGridView1.Columns.Add("Column2", "Рабочие");
                    dataGridView1.Columns.Add("Column3", "Стоимость");
                    dataGridView1.Columns[0].Width = 115;
                    dataGridView1.Columns[1].Width = 115;
                    for (int i = 0; i < MDIParent.p; i++)
                        dataGridView1.Rows.Add("Процесс" + (i + 1));
                    break;
                case 2: //Неритмичный поток
                    dataGridView1.Columns.Add("Column1", "Рабочие");
                    dataGridView1.Columns.Add("Column2", "Стоимость");
                    dataGridView1.Columns[0].Width = 115;
                    DataGridViewTextBoxColumn _col;
                    for (int i = 0; i < (MDIParent.z); i++)
                    {
                        _col = new DataGridViewTextBoxColumn();
                        _col.Name = "Column" + dataGridView1.ColumnCount;
                        _col.HeaderText = (dataGridView1.ColumnCount - 2).ToString();
                        _col.Width = 35;
                        _col.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Programmatic;
                        dataGridView1.Columns.Insert(dataGridView1.Columns.Count - 2, _col);
                    }
                    for (int i = 0; i < MDIParent.p; i++)
                        dataGridView1.Rows.Add("Процесс" + (i + 1));
                    break;
            }
        }

        private void toolStripComboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            int a = toolStripComboBox2.SelectedIndex + minimum;
            mdip.changed = true;
            if (MDIParent.z < a && toolStripComboBox1.SelectedIndex == 2)
            {
                DataGridViewTextBoxColumn _col;
                for (int i = 0; i < (a - MDIParent.z); i++)
                {
                    _col = new DataGridViewTextBoxColumn();
                    _col.Name = "Column" + dataGridView1.ColumnCount;
                    _col.HeaderText = (dataGridView1.ColumnCount - 2).ToString();
                    _col.Width = 35;
                    _col.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Programmatic;
                    dataGridView1.Columns.Insert(dataGridView1.Columns.Count - 2, _col);
                }
            }
            else if (toolStripComboBox1.SelectedIndex == 2)
                for (int i = 0; i < (MDIParent.z - a); i++)
                    dataGridView1.Columns.RemoveAt(dataGridView1.ColumnCount - 3);
            MDIParent.z = toolStripComboBox2.SelectedIndex + 2;
        }

        private void toolStripComboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            int a = toolStripComboBox3.SelectedIndex + minimum;
            mdip.changed = true;
            if (MDIParent.p < a)
                for (int i = 0; i < (a - MDIParent.p); i++)
                    dataGridView1.Rows.Insert(dataGridView1.RowCount, "Процесс" + (dataGridView1.RowCount + 1));
            else
                for (int i = 0; i < (MDIParent.p - a); i++)
                    dataGridView1.Rows.RemoveAt(dataGridView1.RowCount - 1);
            MDIParent.p = toolStripComboBox3.SelectedIndex + 2;
        }

        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyValue == 46)
                foreach (DataGridViewTextBoxCell c in dataGridView1.SelectedCells)
                    c.Value = "";
        }
        
        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            TextBox tb = (TextBox)e.Control;
            if (dataGridView1.CurrentCell.ColumnIndex != 0)
            {               
                tb.KeyPress -= tb_KeyPress;
                tb.KeyPress += new KeyPressEventHandler(tb_KeyPress);
            }
            else
                tb.KeyPress -= tb_KeyPress;
        }

        void tb_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!((e.KeyChar >= (char)48 && e.KeyChar <= (char)57) || (e.KeyChar == (char)8) || (e.KeyChar == (char)46)))
                e.Handled = true;
            else
            {
                mdip.changed = true;
            }
        }
    }
}
